﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using V5_Book.View;
using V5_Book.Model;
using System.Text;
using System.Windows.Resources;
using System.IO;
using V5_PhoneLibs.Core;

namespace V5_Book {
    public partial class MainPage : PhoneApplicationPage {

        private static PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
        // 构造函数
        public MainPage() {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            this.btnGetBook1.Click += new RoutedEventHandler(btnGetBook1_Click);
            this.btnSettings1.Click += new RoutedEventHandler(btnSettings1_Click);
            this.btnSettings2.Click += new RoutedEventHandler(btnSettings2_Click);
        }

        void btnSettings2_Click(object sender, RoutedEventArgs e) {
            UrlHelper.Instance.Action(this, "/View/BookViewSettings.xaml");
        }

        void btnSettings1_Click(object sender, RoutedEventArgs e) {
            UrlHelper.Instance.Action(this, "/View/Settings.xaml");
        }

        void btnGetBook1_Click(object sender, RoutedEventArgs e) {
            UrlHelper.Instance.Action(this, "/View/GetResource/PageGetSocket.xaml");
        }

        // 为 ViewModel 项加载数据
        private void MainPage_Loaded(object sender, RoutedEventArgs e) {
            Bind_DataList();
            this.List1.SelectionChanged += new SelectionChangedEventHandler(List1_SelectionChanged);
        }

        private void Bind_DataList() {
            var list = BookHelper.Instance.ListBookInfo();
            list.Sort(new ModelBookInfoSortBy());
            this.List1.ItemsSource = list;
        }
        public class ModelBookInfoSortBy : IComparer<ModelBookInfo> {
            public int Compare(ModelBookInfo x, ModelBookInfo y) {
                return y.LastTime.CompareTo(x.LastTime);
            }
        }
        void List1_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            ListBox lb = sender as ListBox;
            ModelBookInfo item = lb.SelectedItem as ModelBookInfo;
            if (item != null) {
                UrlHelper.Instance.Action(this, "/View/BookView1.xaml?bookname=" + item.BookName);
            }
        }

        private void btnLoadBook_Click(object sender, EventArgs e) {

        }

        private void btnLoadBook1_Click(object sender, EventArgs e) {
            UrlHelper.Instance.Action(this, "/View/BookView1.xaml");
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e) {

        }

        private void abbBookList_Click(object sender, EventArgs e) {
            UrlHelper.Instance.Action(this, "/View/BookList.xaml");
        }

        private void abbBookSetting_Click(object sender, EventArgs e) {
            UrlHelper.Instance.Action(this, "/View/Settings.xaml");
        }
    }
}